package com.cykj.pos.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cykj.common.annotation.DataSource;
import com.cykj.common.config.RuoYiConfig;
import com.cykj.common.constant.Constants;
import com.cykj.common.core.domain.entity.SysDictData;
import com.cykj.common.core.domain.entity.SysDictType;
import com.cykj.common.enums.DataSourceType;
import com.cykj.common.exception.CustomException;
import com.cykj.common.utils.StringUtils;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.BizPosMachineTemporaryDTO;
import com.cykj.pos.domain.dto.MachineTransferDTO;
import com.cykj.pos.mapper.BizMerchTransactionsMapper;
import com.cykj.pos.mapper.BizMerchantMapper;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.mapper.BizPosMachineTemporaryMapper;
import com.cykj.pos.profit.dto.*;
import com.cykj.pos.service.*;
import com.cykj.pos.util.BigDecimalUtil;
import com.cykj.pos.util.DESHelperUtil;
import com.cykj.pos.util.DateUtils;
import com.cykj.pos.websocket.server.WebSocketServer;
import com.cykj.system.mapper.SysUserMapper;
import com.cykj.system.service.ISysConfigService;
import com.cykj.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 终端设备信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-11
 */
@Service
public class BizPosMachineTemporaryServiceImpl extends ServiceImpl<BizPosMachineTemporaryMapper, BizPosMachineTemporaryDTO> implements IBizPosMachineTemporaryService {

    @Autowired
    IBizMerchBillService bizMerchBillService;

    @Autowired
    IBizWalletService walletService;

    @Autowired
    IBizMerchIntegralService merchIntegralService;

    @Autowired
    IBizPosPolicyService posPolicyService;

    @Autowired
    BizPosMachineMapper posMachineMapper;

    @Autowired
    private ISysConfigService configService;

    @Override
    public String importPosMachine(List<BizPosMachineTemporaryDTO> posList, boolean isUpdateSupport, String operName) {
        //Long userId=userMapper.selectUserId(operName);
        if (StringUtils.isNull(posList) || posList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (BizPosMachineTemporaryDTO posMachine : posList)
        {
            try
            {
                // 验证是否存在这个用户
                BizPosMachine posCode = posMachineMapper.selectPosCode(posMachine.getPosCode());
                BizPosMachineTemporaryDTO posCodeTemporary = posMachineMapper.selectPosTemporaryCode(posMachine.getPosCode());
                if (StringUtils.isNull(posCode) && StringUtils.isNull(posCodeTemporary))
                {
//                    posMachine.setPassword(SecurityUtils.encryptPassword(password));
                    //posMachine.setCreateBy(userId);
                   String posType = posMachineMapper.selectPosType(posMachine.getPosType());
                   if (posType==null || "".equals(posType)){
                       posMachine.setPosType("9");
                   }
                   String var1 = posMachineMapper.selectVar1(posMachine.getVar1());
                   posMachine.setVar1(var1);
                    posMachine.setMerchId((long) 2);
                    posMachine.setPosActivateStatus("0");
                    //bizPosMachineMapper.insertPosMachine(posMachine);
                    saveOrUpdate(posMachine);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、pos机 " + posMachine.getPosCode() + " 导入成功");
                }
                /*else if (isUpdateSupport)
                {
                    //posMachine.setUpdateBy(userId);
                   // bizPosMachineMapper.insertPosMachine(posMachine);
                    saveOrUpdate(posMachine);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、pos机 " + posMachine.getPosCode() + " 更新成功");
                }*/
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、pos机 " + posMachine.getPosCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、pos机 " + posMachine.getPosCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}

